// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database();
  const _ = db.command
  // 师生登录
  if(event.identity=="user") {
    let result= await db.collection("userList").where({
      num:event.obj.num,
      password:event.obj.password
    }).get().then(res=>{
      if (res.data.length==0) {
        return {ststus:0,msg:"账号或密码错误"}
      }else{
        return {status:200,data:res.data};
      }
    })
    return{
      result,
      openid: wxContext.OPENID,
    }
  }
  // 维修工登录
  if(event.identity=="worker") {
    let result= await db.collection("workerList").where({
      num:event.obj.num,
      password:event.obj.password
    }).get().then(res=>{
      if (res.data.length==0) {
        return {ststus:0,msg:"账号或密码错误"}
      }else{
        return {status:200,data:res.data};;
      }
    })
    return{
      result,
      openid: wxContext.OPENID,
    }
  }
  // 管理员登录
  if(event.identity.includes("admin")) {
    let result= await db.collection("adminList").where({
      num:event.obj.num,
      password:event.obj.password,
      identity:_.in(['admin', 'super_admin'])
    }).get().then(res=>{
      if (res.data.length==0) {
        return {ststus:0,msg:"账号或密码错误"}
      }else{
        return {status:200,data:res.data};;
      }
    })
    return{
      result,
      openid: wxContext.OPENID,
    }
  }
  
}